package com.paradogs.framework.core.annoations.tick;

import com.paradogs.framework.core.annoations.base.BaseImportSelector;
import com.paradogs.framework.core.annoations.base.BaseScanner;
import com.paradogs.framework.core.annoations.request.PRMsgMapping;
import com.paradogs.framework.core.annoations.request.PRMsgMappingHolder;
import com.paradogs.framework.core.msg.route.MsgMapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

/**
 * @author: yumi
 * @date: 2023/8/25  16:15
 * @Description: 搜索被 @PRTick 注解修饰的方法
 */
@Slf4j
public class PRTickSelector extends BaseImportSelector {

    @Override
    public BaseScanner getScanner(BeanFactory beanFactory) {
        return new PRTickScanner((DefaultListableBeanFactory) beanFactory);
    }

    @Override
    public void handle(BeanDefinition beanDefinition) {

        try {
            Class<?> clz = Class.forName(beanDefinition.getBeanClassName());
            for (Method method : clz.getDeclaredMethods()) {
                if (AnnotationUtils.findAnnotation(method, PRTick.class) != null) {
                    PRTickHolder.addTickMethod(method);
                }
            }
        } catch (Exception e) {
            log.error("Load class {} error: {}", beanDefinition.getBeanClassName(), e.getMessage(), e);
        }

    }

}
